package com.alcj.controller;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.utils.CmnUtil;
import com.alcj.model.Station;
import com.alcj.model.VehicleType;
import com.alcj.service.StationService;
import com.alcj.service.VehicleTypeService;
import com.alibaba.fastjson.JSONObject;
import org.apache.catalina.Session;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :wangpf
 * @version :v1.0
 * @Package :com.alcj.controller
 * @Description :${TODO}(检测站查询)
 * @date :2017/10/16 0005 上午 11:10
 */
@Controller
@RequestMapping("/searchStation")
public class SearchStationController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    VehicleTypeService vehicleTypeService;

    @Autowired
    StationService stationService;

    /**
     * @Author: wangpf
     * @Description:
     * @Date: 2017/10/16 11:53
     * @param: request
     * @param: model
     */
    @RequestMapping("/init")
    public String getSearchCondition(HttpServletRequest request, Model model) {

        try {
            // 获取车辆类型列表
            List<VehicleType> vehicleTypeList = vehicleTypeService.selectVehicleTypeList();

            // 车辆类型小型车车辆类型ID
            String smallCarId = "";
            // 车辆类型大型车车辆类型ID
            String overSizeVehicleId = "";

            // 循环车辆类型列表
            for(VehicleType vehicleType : vehicleTypeList){

                // 获取小型车的车辆类型ID
                if(vehicleType.getVehicletypename().equals(SysConstant.VEHICLE_TYPE_SMALL)){
                    smallCarId = vehicleType.getVehicletypeid();
                }
                // 获取大型车的车辆类型ID
                if(vehicleType.getVehicletypename().equals(SysConstant.VEHICLE_TYPE_BIG)){
                    overSizeVehicleId = vehicleType.getVehicletypeid();
                }
            }

            // 小型车
            model.addAttribute("smallCarId",smallCarId);
            // 大型车
            model.addAttribute("overSizeVehicleId",overSizeVehicleId);

        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "searchStation";
    }

    /**
     * @Author: wangpf
     * @Description: 根据城市ID获取检测站列表
     * @Date: 2017/10/16 11:53
     * @param: request
     * @return resultJson 检测站列表
     */
    @RequestMapping("/getStationList")
    @ResponseBody
    public JSONObject getStationList(HttpServletRequest request) {
        JSONObject resultJson = new JSONObject();

        try {
            // 城市ID
            String cityCode = request.getParameter("cityCode");
            // 根据城市ID获取该城市的汽车检测站列表
            List<Station> stationList = stationService.selectStationList(cityCode);
            // 检测站列表存在时
            if (!CmnUtil.isEmpty(stationList)){
                resultJson.put("status","0");
                resultJson.put("msg","操作成功");
                resultJson.put("data", JSONObject.toJSON(stationList));

            }else{
                resultJson.put("status","1");
                resultJson.put("msg","该城市无检测站信息");
                resultJson.put("data","");
            }
        }
        catch (Exception e) {
            // 异常报错
            logger.error("获检测站信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            resultJson.put("status","1");
            resultJson.put("msg","获取参数错误！");
            return  resultJson;
        }
        return resultJson;
    }

    /**
     * @Author: wangpf
     * @Description: 获取检测站列表
     * @Date: 2017/10/16 11:53
     * @param: request
     * @return: resultJson 检测站列表
     */
    @RequestMapping("/getStationListByCondition")
    @ResponseBody
    public JSONObject getStationListByCondition(HttpServletRequest request) {

        JSONObject resultJson = new JSONObject();

        try {

            HttpSession session = request.getSession();
            // 车辆类型
            String vehicleType = request.getParameter("vehicleType");
            // 城市ID
            String cityCode = request.getParameter("cityCode");

            String[] areaCodeList;

            if (request.getParameter("areaCodeList") != ""){
                // 区县ID列表
                areaCodeList =  request.getParameter("areaCodeList").split(",");
            }else{
                areaCodeList = null;
            }
            // 区县ID列表
            //String[] areaCodeList =  request.getParameter("areaCodeList").split(",");

            //if (){}
            // 预约日期
            String reservationDay = request.getParameter("reservationDay");
            // 预约开始时间
            String reservationStartTime = request.getParameter("reservationStartTime");
            // 预约结束时间
            String reservationEndTime = request.getParameter("reservationEndTime");

            Map stationSearchCondition = new HashMap();
            stationSearchCondition.put("vehicleType",vehicleType);
            stationSearchCondition.put("cityCode",cityCode);
            stationSearchCondition.put("areaCodeList",areaCodeList);
            stationSearchCondition.put("reservationDay",reservationDay);
            stationSearchCondition.put("reservationStartTime",reservationStartTime);
            stationSearchCondition.put("reservationEndTime",reservationEndTime);

            session.setAttribute("vehicleType",vehicleType);
            session.setAttribute("reservationDay",reservationDay);
            session.setAttribute("reservationStartTime",reservationStartTime);
            session.setAttribute("reservationEndTime",reservationEndTime);

            // 获取汽车检测站列表
            List<Station> stationList = stationService.selectStationListByCondition(stationSearchCondition);

            if (!CmnUtil.isEmpty(stationList)){
                resultJson.put("status","0");
                resultJson.put("msg","操作成功");
                resultJson.put("data", JSONObject.toJSON(stationList));
            }else{
                resultJson.put("status","1");
                resultJson.put("msg","该城市无检测站信息!");
                resultJson.put("data","");
            }
        }
        catch (Exception e){
            // 异常报错
            logger.error("获检测站信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            resultJson.put("status","1");
            resultJson.put("msg","获取参数错误");
            return  resultJson;
        }
        return resultJson;
    }
}